package com.prs.crm.dao.sys.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.sys.MailAddressDao;
import com.prs.crm.domain.sys.MailAddress;

public class MailAddressDaoImpl extends BaseDaoImpl<MailAddress> implements MailAddressDao{

	public MailAddress findByAddr(final String addr)
	{
		return (MailAddress)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						@SuppressWarnings("unchecked")
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(MailAddress.class);
				        	criteria.add(Restrictions.eq("addr", addr));
				        	List<MailAddress> result = criteria.list();
				        	if (result.size()>0)
				        		return result.get(0);
				        	else
				        		return null;
				        }   
				 } );
	}
	
	@SuppressWarnings("unchecked")
	public Collection<MailAddress> getAllAddress()
	{
		return this.getHibernateTemplate().loadAll(MailAddress.class);
	}
}
